<?php
/**
 * 添加会员
 * Created by PhpStorm.
 * User: eric
 * Date: 2020/7/22
 * Time: 12:16
 */

namespace App\Admin\Extensions\Action;

use App\Models\Express;
use App\Models\FarmOrderPick;
use App\Models\Order;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrderDelivery extends RowAction
{

    public $name = '发货';

    protected $selector = '.order-delivery';

    public function handle(Model $model, Request $request)
    {
        //处理逻辑
        $data = $request->input();
        if (empty($data['express_name']) || empty($data['express_number'])) {
            return $this->response()->error('请填写物流名称或单号');
        }
        $rel = Order::where('id', $model->id)->where('status', 1)->where('refund_status', 0)->update(['status' => 2, 'express_name' => $data['express_name'], 'express_number' => $data['express_number'], 'delivery_at' => time()]);
//        $model->express_name   = $data['express_name'];
//        $model->express_number = $data['express_number'];
//        $model->delivery_at    = time();
//        $rel                   = $model->save();
        if (!$rel) {
            return $this->response()->error('发货失败');
        }
        return $this->response()->success('发货成功')->refresh();
    }

    public function form()
    {
        $this->select('express_name', '物流名称')->options(Express::where('status', 1)->pluck('name', 'name as id'));
        $this->text('express_number', '物流单号')->attribute(['autocomplete' => 'off']);
    }

    public function html()
    {
        return <<<HTML
        <a class="btn btn-sm btn-default order-delivery" title="发货">发货</a>
HTML;
    }


}
